﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using System.IO;
using DevExpress.Spreadsheet;

namespace COMOSOFT.OrderBusiness.Forms
{
    public partial class FrmExportExcel : Form
    {
        public FrmExportExcel()
        {
            InitializeComponent();
        }
        public void LoadData(GridView GV)
        {
            MemoryStream sstream = new MemoryStream();
            DevExpress.XtraPrinting.XlsExportOptions xlsOption = new DevExpress.XtraPrinting.XlsExportOptions();
            xlsOption.TextExportMode = DevExpress.XtraPrinting.TextExportMode.Value;
            xlsOption.ShowGridLines = false;
            xlsOption.ExportMode = DevExpress.XtraPrinting.XlsExportMode.SingleFile;

            GV.ExportToXls(sstream, xlsOption);
            spreadsheetControl.LoadDocument(sstream, DocumentFormat.Xls);
            spreadsheetControl.Document.Worksheets.ActiveWorksheet.Columns.AutoFit(1, 100);
            spreadsheetControl.Document.Worksheets.ActiveWorksheet.Rows.AutoFit(1, 100);
            spreadsheetControl.Document.Worksheets.ActiveWorksheet.Rows[17].Alignment.WrapText = true;
            spreadsheetControl.Document.Worksheets.ActiveWorksheet.Rows[0].Height = 50;
        }

        private void dataNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            if (e.Button.Tag.ToString() == "Print")
            {
                spreadsheetControl.ShowPrintPreview();
            }
            else
            {
                string fName = null;
                SaveFileDialog sdlg = new SaveFileDialog();
                sdlg.Filter = "Excell 2003 (.xls)|*.xls| Excell 2007 (.xlsx)|*.xlsx";
                sdlg.FileName = "GaneralView" + DateTime.Now.ToString("ddMMYY");
                if (sdlg.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        fName = sdlg.FileName;
                        spreadsheetControl.SaveDocument(fName, DocumentFormat.Xlsx);
                        if (MessageBox.Show("Open file Export", "Do you want open it?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            System.Diagnostics.Process.Start(fName);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }
    }
}
